package fr.rcp.file.explorer.controleur;

import java.io.File;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.dialogs.InputDialog;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.widgets.Display;

import fr.rcp.file.explorer.views.Console;

/**
 * @author fred
 * 
 */
public class RenameAction extends Action {
	private Console parent;

	public RenameAction(Console parent, String label) {
		this.parent = parent;
		this.setText(label);
		this.setToolTipText("Copier");
	}

	public void run() {
		IStructuredSelection selection = (IStructuredSelection) parent
				.getTableViewer().getSelection();
		File selected_file = (File) selection.getFirstElement();
		int reponse = 0;
		InputDialog inputDialog = new InputDialog(Display.getCurrent()
				.getActiveShell(), "Renommer le fichier",
				"Veuiller inserer le nouveau nom", selected_file.getName(),
				null);
		reponse = inputDialog.open();
		String chemin = selected_file.getPath().substring(0,
				selected_file.getPath().lastIndexOf("\\"));
		if (reponse == Window.OK)
			selected_file.renameTo(new File(chemin + "\\"
					+ inputDialog.getValue()));
		parent.getTableViewer().refresh();

	}
}
